<template>
  <div id="person_home">
    <van-cell-group title="功能" style="text-align: left">
      <van-cell v-if="mode=='1'?true:false" title="扫码查看房间信息" clickable @click="scan" is-link />
      <van-cell v-if="mode=='0'?true:false" title="充值" clickable @click="charge" is-link />
    </van-cell-group>
    <h4 style="text-align: left; margin: 10px; color: #535c68">远程开门</h4>

    <van-grid
      style="margin-top: 10px"
      icon-size="60px"
      :column-num="3"
      clickable
      square
      gutter="10px"
    >
      <van-grid-item
        v-for="item in door_list"
        :key="item.id"
        icon="lock"
        :text="item.door_name"
        @click="openDoor(item.door_name)"
      />
    </van-grid>
  </div>
</template>

<script>
import { Dialog, Toast } from "vant";
export default {
  data() {
    return {
      door_list: [],
      face_status: "",
      mode:this.$store.state.mode
    };
  },
  methods: {
    scan() {
      wx.scanQRCode({
        needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
        success: (res) => {
          if (res.resultStr) {
            Toast.success("扫码成功");
            let house_id = this.$AES.decrypt(res.resultStr);
            this.getHouseDetail(house_id).then((house) => {
              this.setHouse(
                house.house_about_id,
                house.house_about_name,
                house.house_id,
                house.house_nbr
              ).then((res) => {
                if (res) {
                  this.$router.replace("/client/home");
                }
              });
            });
          }
        },
      });
    },
    async getHouseDetail(house_id) {
      return new Promise((resolve) => {
        this.$axios({
          method: "post",
          url: "/api/houseapi/dispatcher",
          data: {
            action: "list_id", //控制参数
            house_id: house_id,
          },
        }).then((res) => {
          if (res.data.ret == 0) {
            resolve(res.data.data[0]);
          }
        });
      });
    },
    async setHouse(building_id, building_name, house_id, house_name) {
      return new Promise((resolve) => {
        let guest_id = this.$AES.decrypt(this.$store.state.tenant.id);
        this.$axios({
          method: "post",
          url: "/api/guestapi/dispatcher",
          data: {
            action: "re", //控制参数
            key: [
              "guest_about_building_id",
              "guest_about_building",
              "guest_about_house_id",
              "guest_about_house",
            ],
            value: [building_id, building_name, house_id, house_name],
            guest_id: guest_id,
          },
        }).then((res) => {
          if (res.data.ret == 0) {
            resolve(true);
          }
        });
      });
    },
    getDoorList() {
      let building_id = this.$AES.decrypt(this.$store.state.tenant.building_id);
      let openid = this.$AES.decrypt(this.$store.state.tenant.openid);
      if (building_id != "null") {
        this.$axios({
          method: "post",
          url: "/api/HIKapi/dispatcher",
          data: {
            action: "list",
            list_type: 1,
            openid: openid,
            about_id: Number(building_id),
          },
        }).then((res) => {
          if (res.data.ret == 0) {
            this.door_list = res.data.data;
          }
        });
      }
    },
    openDoor(name) {
      let openid = this.$AES.decrypt(this.$store.state.tenant.openid);
      this.$axios({
        method: "post",
        url: "/api/HIKapi/dispatcher",
        data: {
          action: "open_door",
          user_mode: Number(this.$store.state.mode),
          door_name: name,
          openid:openid
        },
      }).then((res) => {
        if (res.data.ret == 0) {
          Toast.success("开门成功");
        } else {
          Toast.fail(res.data.msg);
        }
      });
    },
  },
  mounted() {
    this.getDoorList();
  },
};
</script>

<style scoped>
</style>
<style>
#person_home .van-cell-group__title {
  text-align: left;
}

#person_home .van-grid-item__content--clickable .van-icon {
  color: #fff;
}
#person_home .van-grid-item__content--clickable {
  background-color: #6ab04c;
}

#person_home [class*="van-hairline"]::after {
  border: none;
}
#person_home .van-grid-item__text {
  color: #fff;
  font-size: 16px;
}
</style>